package com.effect.part1_ObjectCrateDestroy.item2;

/**
 * Builder Pattern
 * Created by jon on 17-10-7.
 */
public class NutritionFacts_03_Builder {
    private  final  int servingSize;
    private  final  int servings;
    private  final  int calories;
    private  final  int fat;
    private  final  int sodium;
    private  final  int carbohydrate;
    public  static  class  Builder{
        //Required parameters
        private  final  int servingSize;
        private  final  int servings;

        //Optional parameters = initialized to default values
        private  int calories     = 0;
        private  int fat          = 0;
        private  int carbohydrate = 0;
        private  int sodium       = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

        public  Builder calories(int val){
            calories = val;
            return this;
        }
        public  Builder fat(int val){
            this.fat = fat;
            return  this;
        }
        public  Builder carbohydrate(int val){
            carbohydrate = val;
            return  this;
        }
        public  Builder sodium(int val){
            sodium = val;
            return  this;
        }
        public  NutritionFacts_03_Builder build(){
            return  new NutritionFacts_03_Builder(this);
        }
    }

    public NutritionFacts_03_Builder(Builder builder) {
        this.servingSize = builder.servingSize;
        this.servings = builder.servings;
        this.calories = builder.calories;
        this.fat = builder.fat;
        this.sodium = builder.sodium;
        this.carbohydrate = builder.carbohydrate;
    }

    public static void main(String[] args) {
        NutritionFacts_03_Builder cocaCola = new NutritionFacts_03_Builder.Builder(240,9)
                .calories(100).sodium(35).carbohydrate(27).build();
    }
}
